package cn.edu.nju.software.model.administrator;

import java.sql.ResultSet;
import java.sql.SQLException;

import cn.edu.nju.software.model.DatabaseHelper;

public class MemberManager {

	private static DatabaseHelper dbHelper;

	public MemberManager() {
		if (dbHelper == null) {
			dbHelper = new DatabaseHelper();
		}
	}

	public int usableCount() {
		return dbHelper.fetchMemberCount(DatabaseHelper.T_MEMBER_C_STATUS, 1);
	}

	public int unusableCount() {
		return dbHelper.fetchMemberCount(DatabaseHelper.T_MEMBER_C_STATUS, 0);
	}

	public int maleCount() {
		return dbHelper.fetchMemberCount(DatabaseHelper.T_MEMBER_C_GENDER,
				"male");
	}

	public int femaleCount() {
		return dbHelper.fetchMemberCount(DatabaseHelper.T_MEMBER_C_GENDER,
				"female");
	}

	public int a5Count() {
		return dbHelper.fetchMemberCount(DatabaseHelper.T_MEMBER_C_AGE, "50");
	}

	public int a6Count() {
		return dbHelper.fetchMemberCount(DatabaseHelper.T_MEMBER_C_AGE, "60");
	}

	public int a7Count() {
		return dbHelper.fetchMemberCount(DatabaseHelper.T_MEMBER_C_AGE, "70");
	}

	public int a8Count() {
		return dbHelper.fetchMemberCount(DatabaseHelper.T_MEMBER_C_AGE, "80");
	}

	public int a9Count() {
		return dbHelper.fetchMemberCount(DatabaseHelper.T_MEMBER_C_AGE, "90");
	}

	public int a0Count() {
		return dbHelper.fetchMemberCount(DatabaseHelper.T_MEMBER_C_AGE, "00");
	}

	public String[] fetchAllMembers() {
		ResultSet rs = dbHelper.fetchAllRecord(DatabaseHelper.TID_MEMBER);
		try {
			if (rs != null) {
				rs.last();
				int count = rs.getRow();
				rs.beforeFirst();

				String[] members = new String[count];
				for (int i = 0; rs.next(); i++) {
					members[i] = rs.getString(2);
				}
				return members;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public boolean registerMember(String username, String password,
			int levelId, String deadline, int status, int balance,
			String gender, String age) {
		boolean result = false;

		result = true;
		dbHelper.registerMember(username, password, levelId, deadline, status,
				balance, gender, age);

		return result;
	}

	public boolean alterMemberStatus(String username, int levelId,
			String deadline, int status, int balance) {
		boolean result = false;

		result = true;
		dbHelper.alterMemberStatus(username, levelId, deadline, status, balance);

		return result;
	}
}
